package cn.bugstack.springframework.test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class VolatileAtomDemo {

       private static volatile int sum = 0;
//    private static AtomicInteger sum = new AtomicInteger(0);

    public static void main(String[] args) {
        try {
            int times = 20;

            CountDownLatch countDownLatch = new CountDownLatch(times);

            for (int i = 0; i < times; i++) {
                //开启一个线程
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < 100000; i++) {
                            sum++;
                        }
                        countDownLatch.countDown();
                    }
                });
                thread.start();
            }
            countDownLatch.await();
            System.out.println("sum is:" + sum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
